﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cn.Edwon.Data {
    public class DqlUtil {
        /// <summary>
        /// （已废弃）过滤文本数据中的单引号，反斜线和百分号
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static String Decode(String value) {
            if (String.IsNullOrEmpty(value)) return "";
            value = value.Replace("'", "''");
            value = value.Replace("\\", "\\\\");
            value = value.Replace("%", "\\%");
            return value;
        }

        /// <summary>
        /// 过滤文本数据中的单引号，反斜线和百分号
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static String EncodeVal(String value) {
            if (String.IsNullOrEmpty(value)) return "";
            value = value.Replace("'", "''");
            value = value.Replace("\\", "\\\\");
            value = value.Replace("%", "\\%");
            return value;
        }
        /// <summary>
        /// 多个文本类型的用逗号分隔的字符串数据值，分解为可以在IN中表示的文本。
        /// 如aaa,bbb，转换为'aaa','bbb'
        /// </summary>
        /// <param name="rKeys"></param>
        /// <returns></returns>
        public static String FixInValStr(String rVals) {
            if (!String.IsNullOrEmpty(rVals)) {
                String[] vals = rVals.Split(',');
                String val = "";
                for (int i = 0; i < vals.Length; i++) {
                    if (i > 0) val += ",";
                    val += "'" + EncodeVal(vals[i]) + "'";
                }
                return val;
            } else {
                return "";
            }
        }
    }
}
